import java.util.HashSet;
import java.util.Set;

/**
 * @author yexiebao
 * @date 2025/5/18
 */
public class HappyNumber {
    public static boolean isHappy(int n) {
        int sum = 0;
        Set<Integer> set = new HashSet<>();

        while(n != 1 && !set.contains(n)) {
            set.add(n);
            sum =0;
            while (n != 0) {
                int num = n % 10;
                sum += num * num;
                n = n / 10;
            }

            n = sum;
        }
        return n==1;
    }

    public static int getNext(int n){
        int sum = 0;
        while(n!=0){
            int num = n% 10;
            sum = num*num;
            n = n/10;
        }
        return sum;
    }
    public static boolean isHappy2(int n) {
        int slow = n;
        int fast = getNext(n);
        while(fast!=1 && slow!=fast){
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return fast==1;
    }
    public static void main(String[] args) {
        System.out.println(isHappy2(19));
    }
}